﻿<%@ Page Title="数据字典" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet">
<link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="../assets/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="../assets/img/validate/validate.css" rel="stylesheet">
<style type="text/css">
.module-info td .control-label {
    width: 90px;
    text-align: right;
}
@media (min-width: 768px) {
	.modal-dialog {
		width: 768px;
		margin: 30px auto;
	}
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
<div class="system-box">
	<div class="left-side ibox-content">
		<div id="using_json"></div>
	</div>
	<div class="right-side">
		<div class="btns-container">
			<div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
		    	<form id="checkModuleForm" method="get" action="#">
					<div class="modal-dialog">
		        		<div class="modal-content animated bounceInRight">
		            		<div class="modal-header">
		                    	<span class="editTitle">新增模块</span>
		                    	<button type="button" class="close" data-dismiss="modal">
		                    		<span aria-hidden="true">&times;</span>
		                        	<span class="sr-only">关闭</span>
		                        </button>
		                    </div>
		                    <div id="message" style="display: none"></div>
		                    <div class="modal-body">
		                    	<table class="module-info" cellpadding="0" cellspacing="0">
		                        	<thead></thead>
		                        	<tbody>
		                        		<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label">字典名称&nbsp;&nbsp;</label>
													<input type="text" id="DictName" name="DictName" placeholder="请输入字典名称" 
														datacol="yes" err="字典名称" checkexpession="NotNull" 
														maxlength="25" class="form-control input-w630 required" />
												</div>
											</td>
										</tr>
		                            	<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label">　字典值&nbsp;&nbsp;</label>
													<input type="text" id="DictValue" name="DictValue" placeholder="请输入字典值" 
														datacol="yes" err="字典值" checkexpession="NotNull" 
														maxlength="30" class="form-control input-w630 required" />
												</div>
											</td>
										</tr>
										<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label">是否系统设置&nbsp;&nbsp;</label>
													<div class="checkbox i-checks label-inline">
	                                                	<label class="effective">
	                                                		<input id="SystemIdentifier" name="SystemIdentifier" type="checkbox" checked="">
	                                                	</label>
	                                            	</div>
												</div>        
											</td>
										</tr>
		                                <tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label label-float">备注说明&nbsp;&nbsp;</label>
													<textarea id="Memo" name="Memo" style="margin-left: 3px;"
														 class="form-control input-textarea input-w630" placeholder="在此填写备注说明"></textarea>
												</div>
											</td>
										</tr>
		                        	</tbody>
		                    	</table>
		                	</div>
		                	<div class="modal-footer">
		                    	<button type="button" id="addModuleBtn" class="btn btn-primary">保存</button>
		                    	<button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
		                    </div>
		                </div>
		            </div>
		        </form>
		    </div>
		</div>
		<div class="right-content-box">
            <div class="ibox-content">
				<table class="footable table table-stripped table-list">
					<thead></thead>
					<tbody id="pageList"></tbody>
				</table>
	        </div>
        </div>
	</div>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
<script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
<script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
<script src="../assets/js/plugins/footable/footable.all.min.js"></script>
<script src="../assets/js/page/rScroll.js"></script>
<script src="../assets/js/page/tableControl.js"></script>
<script src="../assets/img/validate/learunui-validator.js"></script>
<script>
$(document).ready(function(){
	//限制textarea输入字数
	commonUtil.inputLimitForId('Memo',50);
	//初始化多选框
	$(".i-checks").iCheck({
		checkboxClass:"icheckbox_square-green",
	});

	//模块ID
	var ModuleID = Y.getUrlParam("ModuleID");
	//用于记录当前选中节点字典id（左侧）
	var did = 0;
	//用于记录当前选中节点字典id（右侧）
	var rdid = 0;
	//用于记录当前选中节点父id
	var dParentId = 0;
	//用于记录当前选中节点的名称
	var dName = '';
	//用于记录当前选中节点的值
	var dValue = '';
	//用于标记是新增还是编辑
	var isCreate = true;
	//初始树形列表
	var initTree = true;
	
	//加载左侧列表
	var loadList = function(init) {
		//调用加载层
		layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		
		initTree = true;
		Y.API2('SysDict', 'GetTreeList', {ParentId: 0}, function(data) {
			//关闭加载层
			layer.closeAll('dialog');
			
			var treeData = [];
			if(!Y.checkNull(data) && data.length > 0){
				//json处理
				data.map(function(item,i){
					var temp = {}
					temp.text = item.DictName;
					temp.data = {
						ID: item.ID,
						ParentId: item.ParentId,
						DictValue: item.DictValue,
						DictName: item.DictName
					};
					if(item.Childrens.length > 0){
						temp.state = {opened: true};
						temp.children = getChildNode(item.Childrens);
					}
					treeData.push(temp);
				})
				
				//初始化左侧菜单
				$.jstree.destroy ("#using_json");
				$('#using_json').jstree({
					core: {
						data: treeData
					}
				}).bind('select_node.jstree', function(event, data) {
					initTree = false;
					//绑定的点击事件
					var node_data = data.node.data;
					did = node_data.ID;
					dParentId = node_data.ParentId;
					dName = node_data.DictName;
					dValue = node_data.DictValue;
														
					//如果是根节点则禁用编辑和删除
					if(dParentId == 0){
						$('#editBtn').attr('disabled','disabled');
						$('#deleteBtn').attr('disabled','disabled');
					}else{
						$('#editBtn').removeAttr('disabled');
						$('#deleteBtn').removeAttr('disabled');
					}
					
					//加载右侧列表
					loadPageList(dValue);
				});

				//判断是否为初次调用
				if(!Y.checkNull(init)){
					//获取初始化的相关属性
					did = data[0]['ID'];
					dParentId = data[0]['ParentId'];
					dName = data[0]['DictName'];
					dValue = data[0]['DictValue'];
					$('#editBtn').attr('disabled','disabled');
					$('#deleteBtn').attr('disabled','disabled');
					
					//加载右侧列表
					loadPageList(dValue);
				}
			}
		});
	}
	loadList(true);
	
	//初始化左侧菜单子节点
	var getChildNode = function(data){
		var tempArr = [];
		data.map(function(item,i){
			var temp = {};
			temp.text = item.DictName;
			if(item.Type == 2){
				temp.icon = 'none';
			}
			temp.data = {		
				ID: item.ID,
				ParentId: item.ParentId,
				DictValue: item.DictValue,
				DictName: item.DictName
			};
			if(!Y.checkNull(item.Childrens) && item.Childrens.length > 0){
				temp.state = {opened: false};
				temp.children = getChildNode(item.Childrens);
			}
			tempArr.push(temp);
		});
		return tempArr;
	}
	
	//获取右侧内容列表
	var loadPageList = function (dValue) {
	    if($('.footable thead').children().size() == 0)
	    	tableControl.initThead2('.footable',['字典名字','字典值','状态','备注说明','是否系统设置']);
	    
		Y.API2('SysDict','GetList',{DictValue: dValue},function(data){
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				var tBody = [];
			    if (!Y.checkNull(data)) {
			    	data.map(function(item,i){
						var tempTr = {
							trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd' ),
							children: [
								{tdText: item.ID, c:0 , r:0, tdHtml:'', tdId:'', tdClass:'did hide'},
								{tdText: item.ParentId, c:0 , r:0, tdHtml:'', tdId:'', tdClass:'parentId hide'},
								{tdText: (item.DictName || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
								{tdText: (item.DictValue), c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
								{tdText: (!item.IsRemove ? ' 有效' : ' 无效'), c:0 , r:0, tdHtml: ('<i class="fa fa-'+ (!item.IsRemove ? 'check text-navy' : 'times text-danger') +'"></i>'), tdId:'', tdClass:''},
								{ tdText: (item.Memo || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: (item.SystemIdentifier ? ' 是' : ' 否'), c: 0, r: 0, tdHtml: ('<i class="fa fa-' + (item.SystemIdentifier ? 'check text-navy' : 'times text-danger') + '"></i>'), tdId: '', tdClass: '' },
							]
						};
						tBody.push(tempTr);
					})
				}
			    tableControl.initTbody('.footable',(tBody.length > 0 ? tBody : null),3,null);
			}
		});
	}
	
	//获取按钮
	commonUtil.loadBtnList(ModuleID,function(){
		//禁用编辑和删除
		$('#editBtn').attr('disabled','disabled');
		$('#deleteBtn').attr('disabled','disabled');
		rScroll();
	});
	//刷新按钮
	$('.btns-container').on('click', '#refreshBtn', function () {
		loadList(true);
	});
	//新增按钮
	$('.btns-container').on('click', '#addBtn', function () {
		if(initTree){
			swal('请先在目录树选择节点');
			return false;
		}
		
		$('.editTitle').text('新增模块');
		isCreate = true;
		$(':input', '#checkModuleForm').not(':button, :submit, :reset').val('');
		if (!$('#SystemIdentifier').is(':checked'))
			$('#SystemIdentifier').parent().click();
	});
	//编辑按钮
	$('.btns-container').on('click', '#editBtn', function () {
		$('.editTitle').text('编辑模块');
		isCreate = false;
		var id = ($('#pageList .check').size() == 0 ? did : rdid);
		//获取详细信息
		layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		Y.API2('SysDict', 'GetId', { ID: id }, function (data) {
			layer.closeAll('dialog');
			if (data.Success == false) {
				swal(data.Message, '', 'error');
			} else {
				$('#DictName').val(data.DictName);
				$('#DictValue').val(data.DictValue);
				$('#Memo').val(data.Memo);
				if ($('#SystemIdentifier').is(':checked') != data.SystemIdentifier) {
					$('#SystemIdentifier').parent().click();
			   	}
			}
		});
	});
	//删除按钮
	commonUtil.initDeleteBtnEvent({
		callBack: function () {
			var id = ($('#pageList .check').size() == 0 ? did : rdid);
			Y.API2('SysDict', 'Remove', { ID: id }, function (data) {
				layer.closeAll('dialog');
				if (data.Success == false) {
					swal('删除失败', data.Message, 'error');
				} else {
					swal('删除成功', '该信息已被你永久删除', 'success');
					loadList(true);
				}
			});
		}
	});
	
	$('#pageList').on('click','tr',function(){
		$('#pageList tr').removeClass('check');
		$(this).addClass('check');			
		rdid = parseInt($(this).find('.did').text());	
		dParentId = parseInt($(this).find('.parentId').text());
		//如果是根节点则禁用编辑和删除
		if(dParentId == 0 || isNaN(rdid)){
			$('#editBtn').attr('disabled','disabled');
			$('#deleteBtn').attr('disabled','disabled');
		}else{
			$('#editBtn').removeAttr('disabled');
			$('#deleteBtn').removeAttr('disabled');
		}
	});
	
	//检验提交信息
	$('#addModuleBtn').click(function(){
		if (!CheckDataValid('#checkModuleForm')) {
            return false;
        }
		//检验通过后执行新增或者编辑
		var deal = '';
		var successText = '';
		if(isCreate){
			deal = 'Create';
			successText = '新增成功';
		}else{
			deal = 'Modify';
			successText = '编辑成功';
		}
		var id = ($('#pageList .check').size() == 0 ? did : rdid);
		layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		Y.API2('SysDict', deal, {
			ID: id,
			DictName: $('#DictName').val(),
			DictValue: $('#DictValue').val(),
			ParentId: (isCreate ? did : dParentId),
			Memo: $('#Memo').val(),
			SystemIdentifier: $('#SystemIdentifier').is(":checked"),
		},function(data){
			layer.closeAll('dialog');
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				swal(successText, '', 'success');
				//关闭新建  or 编辑框
				$('#closeAddBox').click();
				//刷新列表
				loadList(true);
			}
		})
	})
})
</script>
</asp:Content>

